package com.drivers.homepage.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.drivers.homepage.domain.DriverOnceOrder;
import com.drivers.homepage.domain.OrderInformation;
import com.drivers.homepage.mapper.DriverOnceOrderMapper;
import com.drivers.homepage.mapper.HistoryOrderMapper;
import com.drivers.homepage.mapper.OrderInformationMapper;
import com.drivers.homepage.mapper.OrderTimeMapper;
import com.drivers.homepage.service.OrderInformationService;
import com.drivers.model.historyorder.vo.OperateVo;
import com.drivers.model.historyorder.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class OrderInformationServiceImpl implements OrderInformationService {

	@Autowired
	private OrderInformationMapper orderInformationMapper;

	@Autowired
	private DriverOnceOrderMapper driverOnceOrderMapper;

	@Autowired
	private HistoryOrderMapper historyOrderMapper;
	@Override
	public OrderInformation getOrder(Integer id) {

		OrderInformation allOrder = orderInformationMapper.selectByPrimaryKey(id);
		return allOrder;
	}

	@Override
	public void updateOrderStatus(String id) {

	}

	@Override
	public DriverOnceOrder getOrderOneTimeItem(String driverId) {
//		这一步先去查本次接单数据
		DriverOnceOrder driverOnceOrder = driverOnceOrderMapper.selectByPrimaryKey(Integer.parseInt(driverId));
		if(ObjUtil.isEmpty(driverOnceOrder)){
			DriverOnceOrder driverOnceOrder1 = new DriverOnceOrder();
			driverOnceOrder1.setOnecePrice(new BigDecimal(0));
			driverOnceOrder1.setSumNum(0);
			driverOnceOrder1.setSumTime("0");
			return driverOnceOrder1;
		}
//		这一步去查询本次运营时长//数据库中已经计算好本次运营时长是多久了//按照下面的思路用实体类接受
		DriverOnceOrder orderOneTimeItem = driverOnceOrderMapper.getOrderOneTimeItem(driverOnceOrder);
//		获取sumNum的值进行set
		orderOneTimeItem.setSumNum(driverOnceOrderMapper.getSumNum(driverOnceOrder));
		System.out.println(orderOneTimeItem);
//		查询本次接单次数//为什么不在上面一起查好呢??
//		如果当前司机是新开启订单,没有已开启的行程记录,怎么办?返回一个空对象过去,但是不能全空,把值设置为0
		if(ObjUtil.isNotEmpty(orderOneTimeItem)){

			if(ObjUtil.isEmpty(orderOneTimeItem.getSumTime())){
				orderOneTimeItem.setSumTime("0");
			}
			if(ObjUtil.isEmpty(orderOneTimeItem.getSumNum())){

				orderOneTimeItem.setSumNum(0);
			}
			if(ObjUtil.isEmpty(orderOneTimeItem.getOnecePrice())){

				orderOneTimeItem.setOnecePrice(new BigDecimal(0));
			}

			return orderOneTimeItem;
		}else {
			DriverOnceOrder driverOnceOrder1 = new DriverOnceOrder();
			driverOnceOrder1.setOnecePrice(new BigDecimal(0));
			driverOnceOrder1.setSumNum(0);
			driverOnceOrder1.setSumTime("0");
			return driverOnceOrder1;
		}

	}

	@Override
	public Integer createTodayOrderItem(String driverId) {
//		创建单次订单设置一些属性值,联调
		DriverOnceOrder driverOnceOrder = new DriverOnceOrder();
		driverOnceOrder.setDriverId(Integer.parseInt(driverId));
//		这一步设置开启接单的状态因为前台点击开始接单了,创建一个新的状态做展示
		driverOnceOrder.setStatus(1);
		driverOnceOrder.setStartTime(new Date());
		driverOnceOrder.setSumNum(0);
		driverOnceOrder.setSumTime("0");
		driverOnceOrder.setOnecePrice(new BigDecimal(0));
		driverOnceOrderMapper.updateStatus(driverId);
		int i = driverOnceOrderMapper.insertSelective(driverOnceOrder);
		return driverOnceOrder.getId();
	}

	@Override
	public Integer updateTodayOrderStatus(String id) {
//		new
		DriverOnceOrder driverOnceOrder = new DriverOnceOrder();
//		通过id去更新
		driverOnceOrder.setId(Integer.parseInt(id));
//订单表状态为0未开启
		driverOnceOrder.setStatus(0);
		int i = driverOnceOrderMapper.updateByPrimaryKeySelective(driverOnceOrder);
		return i;
	}


	/**
	 *
	 * 更新订单次数
	 * @param id
	 * @return
	 */
	@Override
	public Integer updateNum(String id) {
		return driverOnceOrderMapper.updateNum(id);
	}

	/**
	 *
	 * 根据公里数计算价格
	 * @param orderId
	 * @return
	 */
	@Override
	public BigDecimal getmoney(String orderId) {

		OrderVo orderVo = new OrderVo();
		orderVo.setOrderCost(historyOrderMapper.findOrderCost(orderId));
		orderVo.setOrderTime(historyOrderMapper.findOrderTime(orderId));
		orderVo.setOrderInformation(historyOrderMapper.findone(orderId));
		// 获取接单时间
		Date date1 = new Date(String.valueOf(orderVo.getOrderTime().getReceiveTime()));
		//转成毫秒
		long milliseconds1 = date1.getTime ();
		//获取到达时间
		Date date2 = new Date(String.valueOf(orderVo.getOrderTime().getArriveTime()));
		//转成毫秒
		long milliseconds2 = date2.getTime ();
		//计算秒差
		long c=(milliseconds1-milliseconds2)/1000;

		if(c%60!=0){
			c=c/60+1;
		}else{
			c=c/60;
		}
		//获取计算金额设置
		OperateVo operateVo = new OperateVo();
		operateVo.setOperatePassengerCharges(historyOrderMapper.findpass());
		operateVo.setOperateDriverCharges(historyOrderMapper.finddriver());
		BigDecimal totalAmount = new BigDecimal(0);
		//在免费等待时间内
		if(c<=operateVo.getOperatePassengerCharges().getDepartureWaitingTime()){
			//订单里程小于起步公里
			if(Integer.valueOf(orderVo.getOrderInformation().getOrderMileage() ) <=operateVo.getOperatePassengerCharges().getStartingKilometer()){
				totalAmount = totalAmount.add(operateVo.getOperatePassengerCharges().getStartingPrice());
			}
			//订单里程大于起步公里
			if(Integer.valueOf(orderVo.getOrderInformation().getOrderMileage())>operateVo.getOperatePassengerCharges().getStartingKilometer()){
				totalAmount = totalAmount.add(operateVo.getOperatePassengerCharges().getStartingPrice());
				totalAmount = totalAmount.add(new BigDecimal(Integer.valueOf(orderVo.getOrderInformation().getOrderMileage())-operateVo.getOperatePassengerCharges().getStartingKilometer()).multiply(operateVo.getOperatePassengerCharges().getMileageFee()));
			}

		}
		//超出免费等待时间内
		if (c>operateVo.getOperatePassengerCharges().getDepartureWaitingTime()){
			//订单里程小于起步公里
			if(Integer.valueOf(orderVo.getOrderInformation().getOrderMileage() )<=operateVo.getOperatePassengerCharges().getStartingKilometer()){

				totalAmount  = totalAmount.add(operateVo.getOperatePassengerCharges().getStartingPrice());
				totalAmount = totalAmount.add(new BigDecimal(c-operateVo.getOperatePassengerCharges().getDepartureWaitingTime()).multiply(operateVo.getOperatePassengerCharges().getDepartureWaitingPrice()));
			}
			//订单里程大于起步公里
			if(Integer.valueOf(orderVo.getOrderInformation().getOrderMileage())>operateVo.getOperatePassengerCharges().getStartingKilometer()){
				totalAmount  = totalAmount.add(operateVo.getOperatePassengerCharges().getStartingPrice());
				totalAmount  = totalAmount.add(new BigDecimal(c-operateVo.getOperatePassengerCharges().getDepartureWaitingTime()).multiply(operateVo.getOperatePassengerCharges().getDepartureWaitingPrice()));
				totalAmount = totalAmount.add(new BigDecimal(Integer.valueOf(orderVo.getOrderInformation().getOrderMileage())-operateVo.getOperatePassengerCharges().getStartingKilometer()).multiply(operateVo.getOperatePassengerCharges().getMileageFee()));
			}
		}

		return totalAmount;
	}



	//注入订单时间表mapper
	@Autowired
	private OrderTimeMapper orderTimeMapper;
	//接单
	@Override
	public void updateOrders(String orderId, Integer driverId) {
		//1.修改订单的司机id
		orderInformationMapper.updateDriverId(orderId,driverId);
		//2.修改订单的状态 被接单
		orderInformationMapper.updateOrderStatus(orderId,1);
		//3.修改订单的接单时间
		orderTimeMapper.updateTakeorderTime(orderId,new Date());
	}
	//司机到达起点
	@Override
	public void updateGet(String orderId) {
		//修改司机到达时间
		orderTimeMapper.updateArriveTime(orderId,new Date());
	}

	//司机接到乘客
	@Override
	public void updateReceive(String orderId) {
		//1.修改司机接到乘客时间
		orderTimeMapper.updateReceiveTime(orderId,new Date());
		//2.修改订单状态为行程中 2
		orderInformationMapper.updateOrderStatus(orderId,2);
	}

	@Override
	public BigDecimal getdrivermoney(BigDecimal jg) {
		//获取计算金额设置
		OperateVo operateVo = new OperateVo();
		operateVo.setOperatePassengerCharges(historyOrderMapper.findpass());
		operateVo.setOperateDriverCharges(historyOrderMapper.finddriver());
		BigDecimal totalAmount = new BigDecimal(0);
		if(operateVo.getOperateDriverCharges().getServiceType()==1){
			if(operateVo.getOperateDriverCharges().getManageType()==1){
				totalAmount= jg.subtract(jg.multiply(
								BigDecimal.valueOf(operateVo.getOperateDriverCharges().getServiceProportion()).divide(BigDecimal.valueOf(100))
						))
						.subtract(jg.multiply(BigDecimal.valueOf(operateVo.getOperateDriverCharges().getManageProportion())).divide(BigDecimal.valueOf(100)))
						.subtract(operateVo.getOperateDriverCharges().getInsurancePrice());
			}
			if(operateVo.getOperateDriverCharges().getManageType()==2){
				totalAmount= jg.subtract(operateVo.getOperateDriverCharges().getInsurancePrice())
						.subtract(operateVo.getOperateDriverCharges().getManagePrice())
						.subtract(jg.multiply(
								BigDecimal.valueOf(operateVo.getOperateDriverCharges().getServiceProportion()).divide(BigDecimal.valueOf(100))
						));
			}
		}
		if(operateVo.getOperateDriverCharges().getServiceType()==2){
			if(operateVo.getOperateDriverCharges().getManageType()==1){
				totalAmount= jg.subtract(operateVo.getOperateDriverCharges().getServicePrice())
						.subtract(jg.multiply(BigDecimal.valueOf(operateVo.getOperateDriverCharges().getManageProportion()).divide(BigDecimal.valueOf(100))))
						.subtract(operateVo.getOperateDriverCharges().getInsurancePrice());
			}
			if(operateVo.getOperateDriverCharges().getManageType()==2){
				totalAmount= jg.subtract(operateVo.getOperateDriverCharges().getServicePrice())
						.subtract(operateVo.getOperateDriverCharges().getManagePrice())
						.subtract(operateVo.getOperateDriverCharges().getInsurancePrice());
			}
		}
		return totalAmount;
	}


}
